package cn.mw.cmdb.service.impl;

import cn.mw.cmdb.entity.CustomClassificationVo;
import cn.mw.cmdb.entity.ModelGroupInfo;
import cn.mw.cmdb.mongoMapper.CustomClassificationMapper;
import cn.mw.cmdb.mongoMapper.ModelInfoMapper;
import cn.mw.cmdb.util.CMDBTool;
import cn.mw.components.mongodb.entity.SearchParam;
import cn.mw.components.mongodb.tool.CriteriaWrapper;
import cn.mw.microMonitorCommon.api.common.MwTPServerService;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

public class CustomClassificationServiceImpl extends ModelBaseServiceImpl<CustomClassificationVo, Object> {

    public static final String ID = "customClassificationService";

    private CustomClassificationMapper customClassificationMapper;

    private ModelInfoMapper modelInfoMapper;

    private MwTPServerService mwTPServerService;

    public CustomClassificationServiceImpl(CustomClassificationMapper customClassificationMapper, ModelInfoMapper modelInfoMapper, MwTPServerService mwTPServerService) {
        this.customClassificationMapper = customClassificationMapper;
        this.modelInfoMapper = modelInfoMapper;
        this.mwTPServerService = mwTPServerService;
    }

    @Override
    public CustomClassificationVo genObject() {
        return new CustomClassificationVo();
    }

    @Override
    Object doAdd(CustomClassificationVo param) throws Exception {
        return customClassificationMapper.insert(param);
    }

    @Override
    boolean doUpdate(CustomClassificationVo param) throws Exception {
        Query query = new Query(CriteriaWrapper.where(ModelGroupInfo::getId).is(param.getId()));
        Update update = CMDBTool.genMongoUpdate(param, null, null);
        CMDBTool.updateFirst(customClassificationMapper, query, update);
        return true;
    }

    @Override
    boolean doRemove(CustomClassificationVo param) throws Exception {
        CMDBTool.remove(customClassificationMapper, param.getId(), CustomClassificationVo.class, false);
        return true;
    }

    @Override
    Object doBatchAdd(Object params) {
        return false;
    }

    @Override
    boolean doBatchUpdate(Object params) {
        return false;
    }

    @Override
    public CustomClassificationVo doSelectById(Object id) {
        return customClassificationMapper.findById(id);
    }

    @Override
    public List<CustomClassificationVo> doSelectList(Object param) throws Exception {
        SearchParam searchParam = (SearchParam) param;
        //获取参数值对对应属性类型,并生成查询条件
        Query query = genQuery(searchParam, null);
        List<CustomClassificationVo> ret = CMDBTool.selectList(CustomClassificationVo.class, customClassificationMapper, query, searchParam);
        return ret;
    }

    @Override
    boolean doBatchRemove(Object params) throws Exception {
        return false;
    }
}
